package com.zhiyou100.crm.servlet.message;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.zhiyou100.crm.service.MessageService;
import com.zhiyou100.crm.service.UserService;
import com.zhiyou100.crm.service.impl.MessageServiceImpl;
import com.zhiyou100.crm.service.impl.UserServiceImpl;
import com.zhiyou100.crm.util.AdminBaseServlet;
import com.zhiyou100.crm.util.Message;
import com.zhiyou100.crm.util.User;

@WebServlet(urlPatterns = { "/message/save" })
public class SaveServlet extends AdminBaseServlet {
	private static final long serialVersionUID = 1L;
	
	MessageService messageService = new MessageServiceImpl();
	UserService userService = new UserServiceImpl();
       
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		List<User> users = userService.queryAllUser();
		request.setAttribute("users",users);
		request.getRequestDispatcher("/WEB-INF/view/message/save.jsp").forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		User user = (User) request.getSession().getAttribute("user");
		int receiver = Integer.parseInt(request.getParameter("receiver"));
		String subject = request.getParameter("subject");
		String content = request.getParameter("content");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long l = System.currentTimeMillis();
        String format = df.format(new Date(l));
        Timestamp time=Timestamp.valueOf(format);
		// 注意这里判断用户点哪个按扭的方法！发送
		boolean send = request.getParameter("send") != null;
		//保存草稿
		boolean save =request.getParameter("save")!=null;
		if(send){
		    //发送
			Message message=new Message(subject,content,receiver,time,user.getUser_id());
            int a = messageService.insert(message);
            if(a>0){
                System.out.println("成功");
//                response.sendRedirect(request.getContextPath()+"/WEB-INF/view/message/listSend.jsp");
				request.getRequestDispatcher("/message/listSend").forward(request,response);
//				response.sendRedirect(request.getHeader("Referer"));
            }
        }else if(save){
			//保存
			Message message=new Message(subject,content,time,user.getUser_id(),receiver);
            int b = messageService.insert1(message);
            if(b>0){
//                response.sendRedirect(request.getContextPath()+"/WEB-INF/view/message/listSave.jsp");
				request.getRequestDispatcher("/message/listSave").forward(request,response);
//				response.sendRedirect(request.getHeader("Referer"));

            }
        }else {
            System.out.println("失败");
            response.sendRedirect(request.getContextPath()+"/list.jsp");
		}

		
	}

}
